package com.wtwd.wtwddevice.deviceserver.netty;


import com.wtwd.device.util.StringUtils;
import com.wtwd.wtwddevice.deviceserver.model.DeviceSession;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用于存储设备会话
 */
public class LocalSessions {

    private final static int INIT_CAPACITY = 2 << 4;

    /**
     * 建立连接后的会话
     */
    private static Map<String, DeviceSession> CONNECT_SESSION = new ConcurrentHashMap<>(INIT_CAPACITY);

    /**
     * 登录成功会话
     */
    private static Map<String, DeviceSession> LOGIN_SESSION = new ConcurrentHashMap<>(INIT_CAPACITY);

    /**
     * 添加管道属性key值
     */
    private static AttributeKey<String> USER_KEY_CHANNEL = AttributeKey.valueOf("key");

    /**
     * 记录设备建立连接且未登录成功时的状态
     *
     * @param deviceKey
     * @param deviceSession
     */
    public static void channelEstablishment(String deviceKey, DeviceSession deviceSession) {
        CONNECT_SESSION.put(deviceKey, deviceSession);
    }

    /**
     * 设备登录
     * 状态
     * 连接---》登录成功
     *
     * @param deviceKey
     */
    public static DeviceSession deviceLoginSession(String deviceKey) {
        DeviceSession deviceSession = CONNECT_SESSION.get(deviceKey);
        if (deviceSession == null) {//连接session为空,说明连接异常丢失
            return null;
        }
        DeviceSession session = LOGIN_SESSION.get(deviceKey);
        //如果通道存在,则关闭之前的通道，以后面的通道为准
        if (session != null) {
            Channel channel = session.getChannel();
            if (channel != null) {
                channel.close();
            }
        }
        deviceSession.getChannel().attr(USER_KEY_CHANNEL).set(deviceKey);
        LOGIN_SESSION.put(deviceKey, deviceSession);
        CONNECT_SESSION.remove(deviceKey);
        return deviceSession;
    }

    /**
     * 获取设备会话
     *
     * @param deviceKey
     * @return
     */
    public static DeviceSession getDeviceSession(String deviceKey) {
        DeviceSession deviceSession = CONNECT_SESSION.get(deviceKey);
        if (deviceSession == null) {
            deviceSession = LOGIN_SESSION.get(deviceKey);
        }
        return deviceSession;
    }

    public static DeviceSession getDeviceLoginSession(String deviceKey) {
        return LOGIN_SESSION.get(deviceKey);
    }

    public static String getDeviceKeyByChannel(Channel channel) {
        for (Map.Entry<String, DeviceSession> deviceSessionData : LOGIN_SESSION.entrySet()) {
            DeviceSession deviceSession = deviceSessionData.getValue();
            if (deviceSession.getChannel() == channel) {
                return deviceSession.getImei();
            }
        }
        return null;
    }

    public static String getDeviceKeyByChannelAttr(Channel channel) {
        return channel.attr(USER_KEY_CHANNEL).get();
    }

    public static String removeByChannel(Channel channel) {
        String deviceKey = getDeviceKeyByChannel(channel);
        if (StringUtils.isNotEmpty(deviceKey)) {
            LOGIN_SESSION.remove(deviceKey);
        }
        return deviceKey;
    }
}

